﻿using System.Collections.Generic;
using System.Data.SqlClient;

namespace BLL.Services.WikiPages
{
    public class WikiPageService : IWikiPageService
    {
        private readonly WikiPageCollection _wikiPageCollection;

        public WikiPageService(WikiPageCollection wikiPageCollection)
        {
            _wikiPageCollection = wikiPageCollection;
        }

        public WikiPage GetById(int wikiPageId)
        {
            try
            {
                return _wikiPageCollection.GetById(wikiPageId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public WikiPage GetByPublicUrl(string publicUrl)
        {
            try
            {
                return _wikiPageCollection.GetByPublicUrl(publicUrl);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Create(WikiPage model)
        {
            try
            {
                _wikiPageCollection.Create(model);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Save(WikiPage model)
        {
            try
            {
                _wikiPageCollection.Save(model);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Delete(int pageId)
        {
            try
            {
                _wikiPageCollection.Delete(pageId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public IEnumerable<WikiPage> GetSomePagesByWikiCategory(
            string wikiCategoryName, string sortOption, int sortDirection, int page = 1, int size = 12)
        {
            try
            {
                return _wikiPageCollection.GetSomePagesByWikiCategory(wikiCategoryName, page, size, sortOption, sortDirection);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public Listing.WikiPageListing GetWikiPageListing(int page, int size, string column, int direction, Listing.WikiPageFilter filter)
        {
            try
            {
                return _wikiPageCollection.GetWikiPagesListing(page, size, column, direction, filter);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }
    }
}
